﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT_Nhom1A.BUS;
using TMDT_Nhom1A.DTO;

namespace TMDT_Nhom1A.Administrator
{
    public partial class ChiTietSanPham_Admin : System.Web.UI.Page
    {
        private static int ParentCatalogueID = 0;
        private static int ChildCatalogueID = 0;
        private static ElectricDevice_DTO electricdevice;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ElectricDevice_BUS.CatalogueIDOfElectricDevice(Request.QueryString["ElectricDeviceID"], ref ParentCatalogueID, ref ChildCatalogueID);
                List<ElectricDevice_DTO> list = ElectricDevice_BUS.DetailElectricDevice(Request.QueryString["ElectricDeviceID"]);
                dldetailproduct.DataSource = list;
                dldetailproduct.DataBind();
                electricdevice = list[0];
                BindData();
            }
        }

        private void BindData()
        {
            gvcoupondevice.DataSource = Coupon_BUS.ListCouponByElectricDeviceID(Request.QueryString["ElectricDeviceID"]);
            gvcoupondevice.DataBind();
        }

        protected void gvcoupondevice_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbStatus = (Label)e.Row.FindControl("lbstatus");
                Label lbinformation = (Label)e.Row.FindControl("lbinformation");
                LinkButton lbselectcoupon = (LinkButton)e.Row.FindControl("lbselectcoupon");
                if (lbStatus.Text == "1")
                {
                    lbselectcoupon.Visible = true;
                    lbinformation.Text = "Còn hạn";
                }
                else
                {
                    lbselectcoupon.Visible = false;
                    lbinformation.Text = "Hết hạn";
                }
            }
        }

        protected void gvcoupondevice_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "CheckAll")
            {
                CheckBox cbcheckall = (CheckBox)gvcoupondevice.HeaderRow.FindControl("cbCheckAll");
                if (cbcheckall.Checked == true)
                {
                    for (int i = 0; i < gvcoupondevice.Rows.Count; i++)
                    {
                        CheckBox cbcheck = (CheckBox)gvcoupondevice.Rows[i].FindControl("cbcheck");
                        cbcheck.Checked = false;
                        cbcheckall.Checked = false;
                    }
                }
                else
                {
                    for (int i = 0; i < gvcoupondevice.Rows.Count; i++)
                    {
                        CheckBox cbcheck = (CheckBox)gvcoupondevice.Rows[i].FindControl("cbcheck");
                        cbcheck.Checked = true;
                        cbcheckall.Checked = true;
                    }
                }
            }
            if (e.CommandName == "SelectCoupon")
            {
                COUPON coupon = Coupon_BUS.DetailCoupon(e.CommandArgument.ToString());
                lbcouponid.Text = coupon.Coupon_ID;
                tbdate.Text = coupon.Date.ToString();
                tbtimetostartselling.Text = coupon.TimeToStartSelling.ToString();
                tbtimetoendselling.Text = coupon.TimeToEndSelling.ToString();
                tbtimetostartusing.Text = coupon.TimeToStartUsing.ToString();
                tbtimetoendusing.Text = coupon.TimeToEndUsing.ToString();
                tbdiscountprice.Text = coupon.DiscountPrice.ToString();
                tbnondiscountprice.Text = coupon.NonDiscountPrice.ToString();
                tbquantityfordiscountprice.Text = coupon.QuantityForDiscountPrice.ToString();
                tbdescription.Text = coupon.Description;
                tbusingcondition.Text = coupon.UsingCondition;
                btaddcoupon.Visible = false;
                btupdate.Visible = true;
                btcancel.Visible = true;
            }
        }

        protected void btactive_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gvcoupondevice.Rows.Count; i++)
            {
                CheckBox cbcheck = (CheckBox)gvcoupondevice.Rows[i].FindControl("cbcheck");
                if (cbcheck.Checked == true)
                {
                    LinkButton lbselectcoupon = (LinkButton)gvcoupondevice.Rows[i].FindControl("lbselectcoupon");
                    Coupon_BUS.UpdateStatusCoupon(lbselectcoupon.CommandArgument, 1);
                }
            }
            BindData();
        }

        protected void btdisactive_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gvcoupondevice.Rows.Count; i++)
            {
                CheckBox cbcheck = (CheckBox)gvcoupondevice.Rows[i].FindControl("cbcheck");
                if (cbcheck.Checked == true)
                {
                    LinkButton lbselectcoupon = (LinkButton)gvcoupondevice.Rows[i].FindControl("lbselectcoupon");
                    Coupon_BUS.UpdateStatusCoupon(lbselectcoupon.CommandArgument, 0);
                }
            }
            BindData();
        }

        protected void btaddcoupon_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void btupdate_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                COUPON coupon = new COUPON();
                coupon.Coupon_ID = lbcouponid.Text;
                coupon.Date = DateTime.Parse(tbdate.Text);
                coupon.TimeToStartSelling = DateTime.Parse(tbtimetostartselling.Text);
                coupon.TimeToEndSelling = DateTime.Parse(tbtimetoendselling.Text);
                coupon.TimeToStartUsing = DateTime.Parse(tbtimetostartusing.Text);
                coupon.TimeToEndUsing = DateTime.Parse(tbtimetostartusing.Text);
                coupon.QuantityForDiscountPrice = int.Parse(tbquantityfordiscountprice.Text);
                coupon.DiscountPrice = tbdiscountprice.Text;
                coupon.NonDiscountPrice = tbnondiscountprice.Text;
                coupon.Description = tbdescription.Text;
                coupon.UsingCondition = tbusingcondition.Text;
                Coupon_BUS.UpdateCoupon(coupon);
                BindData();
                btcancel_Click(sender, e);
            }
            catch (Exception ex)
            {
                Alert.Show("Dữ liệu không đúng !");
            }

        }

        protected void btcancel_Click(object sender, ImageClickEventArgs e)
        {
            tbdate.Text = "";
            lbcouponid.Text = "";
            tbtimetostartselling.Text = "";
            tbtimetoendselling.Text = "";
            tbtimetostartusing.Text = "";
            tbtimetoendusing.Text = "";
            tbdiscountprice.Text = "";
            tbnondiscountprice.Text = "";
            tbquantityfordiscountprice.Text = "";
            tbdescription.Text = "";
            tbusingcondition.Text = "";
            btaddcoupon.Visible = true;
            btupdate.Visible = false;
            btcancel.Visible = false;
        }

        protected void dldetailproduct_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                pncatalogue.Visible = true;
                dlparentcatalogue.DataSource = ParentCatalogue_BUS.ListKeyCatalogue();
                dlparentcatalogue.DataValueField = "ParentCatalogue_ID";
                dlparentcatalogue.DataTextField = "ParentCatalogue_Name";
                dlparentcatalogue.DataBind();
                for (int i = 0; i < dlparentcatalogue.Items.Count; i++)
                {
                    if (int.Parse(dlparentcatalogue.Items[i].Value) == ParentCatalogueID)
                    {
                        dlparentcatalogue.Items[i].Selected = true;
                    }
                }
                dlparentcatalogue_SelectedIndexChanged(source, e);
                dldetailproduct.EditItemIndex = 0;
                dldetailproduct.DataSource = ElectricDevice_BUS.DetailElectricDevice(Request.QueryString["ElectricDeviceID"]);
                dldetailproduct.DataBind();
            }
            if (e.CommandName == "Cancel")
            {
                pncatalogue.Visible = false;
                dldetailproduct.EditItemIndex = -1;
                dldetailproduct.DataSource = ElectricDevice_BUS.DetailElectricDevice(Request.QueryString["ElectricDeviceID"]);
                dldetailproduct.DataBind();
            }
            if (e.CommandName == "Update")
            {
            }
        }

        protected void dlparentcatalogue_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            dlchildrencatalogue.DataSource = ChildCatalogue_BUS.ListCatalogue(int.Parse(dlparentcatalogue.SelectedValue));
            dlchildrencatalogue.DataTextField = "ChildCatalogue_Name";
            dlchildrencatalogue.DataValueField = "ChildCatalogue_ID";
            dlchildrencatalogue.DataBind();
            for (int i = 0; i < dlchildrencatalogue.Items.Count; i++)
            {
                if (int.Parse(dlchildrencatalogue.Items[i].Value) == ChildCatalogueID)
                {
                    dlchildrencatalogue.Items[i].Selected = true;
                }
            }
        }

        protected void dldetailproduct_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            DropDownList dlbrand = (DropDownList)e.Item.FindControl("dlbrand");
            if (dlbrand != null)
            {
                dlbrand.DataSource = Brand_BUS.ListBrand();
                dlbrand.DataTextField = "Brand_Name";
                dlbrand.DataValueField = "Brand_ID";
                dlbrand.DataBind();
                for (int i = 0; i < dlbrand.Items.Count; i++)
                {
                    if (int.Parse(dlbrand.Items[i].Value) == electricdevice.Brand_ID)
                    {
                        dlbrand.Items[i].Selected = true;
                    }
                }
            }
            
        }




    }
}
